package com.aw.baselib.base

import android.app.Activity
import android.app.Application
import android.graphics.Typeface
import android.os.Bundle
import com.aw.baselib.util.AwLog.d
import com.aw.baselib.util.AwNetWatchdog
import com.aw.baselib.util.update.AwFileDownloadUtil.Companion.get
import okhttp3.Call

/**
 * Created by hzw
 */
abstract class AwBaseApplication : Application() {
    private var activityCount = 0
    var typefaceRegular: Typeface? = null
        protected set
    var typefaceMedium: Typeface? = null
        protected set
    var typefaceLight: Typeface? = null
        protected set
    var typefaceBold: Typeface? = null
        protected set

    override fun onCreate() {
        super.onCreate()
        instance = this
        checkNetwork()
        initTypeface()
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                //                AwLog.d("===> onActivityCreated: " + activity.getClass().getName());
            }

            override fun onActivityStarted(activity: Activity) {
                activityCount++
                d("===> onActivityStarted: " + activity.javaClass.name)
                currentActivity = activity
                setIsForeground()
            }

            override fun onActivityResumed(activity: Activity) {}
            override fun onActivityPaused(activity: Activity) {}
            override fun onActivityStopped(activity: Activity) {
                activityCount--
                setIsForeground()
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
            override fun onActivityDestroyed(activity: Activity) {}
        })
    }

    protected abstract fun needReLogin()
    protected abstract val isAllowShowReloginDialog: Boolean
    private fun checkNetwork() {
        netWatchdog = AwNetWatchdog(this)
        netWatchdog!!.setNetConnectedListener(object : AwNetWatchdog.NetConnectedListener {
            override fun onReNetConnected(isReconnect: Boolean) {}
            override fun onNetUnConnected() {
                if (downCalls == null || downCalls!!.size == 0) {
                    return
                }
                for (key in downCalls!!.keys) {
                    get()!!
                        .cancel(instance!!, key)
                }
                //                showDialogToFinish(getString(R.string.common_network_offline));
            }
        })
        netWatchdog!!.startWatch()
    }

    fun cancelNetWatchdog() {
        if (netWatchdog != null) {
            netWatchdog!!.stopWatch()
        }
    }

    protected fun initTypeface() {
        typefaceRegular = Typeface.createFromAsset(assets, "fonts/Montserrat-Regular.ttf")
        typefaceMedium = Typeface.createFromAsset(assets, "fonts/Montserrat-Medium.ttf")
        typefaceLight = Typeface.createFromAsset(assets, "fonts/Montserrat-Light.ttf")
        typefaceBold = Typeface.createFromAsset(assets, "fonts/Montserrat-SemiBold.ttf")

//        typefaceRegular = Typeface.create("sans-serif", Typeface.NORMAL);
//        typefaceMedium = Typeface.create("sans-serif-medium", Typeface.NORMAL);
//        typefaceLight = Typeface.createFromAsset(getAssets(), "fonts/Alibaba-PuHuiTi-Light.otf");
//        typefaceBold = Typeface.create("sans-serif-medium", Typeface.BOLD);
    }

    protected fun setIsForeground() {
        if (activityCount > 0) {
            isForeground = true
        } else {
            isForeground = false
        }
    }

    companion object {
        @JvmStatic
        var instance: AwBaseApplication? = null
            private set
        var downCalls: HashMap<String, Call>? = HashMap()
        var netWatchdog: AwNetWatchdog? = null
        var currentActivity: Activity? = null
        var isForeground = false
    }
}